<?php

namespace apiend\controllers;

use Yii;
use yii\rest\Controller;
use yii\web\UnauthorizedHttpException;
use apiend\models\AccessToken;
use apiend\models\AuthWechatForm;

/**
 * 接口访问授权
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class AuthController extends Controller {

    /**
     * Declares the allowed HTTP verbs.
     * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
     * @return array the allowed HTTP verbs.
     */
    protected function verbs() {
        return [
            'authorize' => ['POST'],
        ];
    }

    /**
     * 接口授权
     * @return mixed
     */
    public function actionAuthorize() {
        $model = new AccessToken();
        $model->load(Yii::$app->request->post(), '');
        $accessToken = $model->authent($msg);
        if ($accessToken == false) {
            throw new UnauthorizedHttpException($msg);
        }
        return $accessToken;
    }

    /**
     * 微信小程序接口授权
     * @return mixed
     */
    public function actionWechat() {
        $model = new AuthWechatForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('请输入帐号和密码信息');
        }
        return $model->authorize();
    }

}
